<?php
require_once "class.db.php";

class sanpham extends db {
    /* SAN PHAM */

    function SanPham_Them(&$loi) {

        $thanhcong = true;
        $idTL = (int) $_POST['idTL'];
        $idLT = (int) $_POST['idLT'];
        
        $MaSP = $this->processData($_POST['MaSP']);
        $TenSP = $this->processData($_POST['TenSP']);
        $TenSP_KD = $this->changeTitle($TenSP);
        $Gia = $this->processData($_POST['Gia']);
       

        $UrlHinh = $this->processData($_POST['UrlHinh']);
        
        $MoTa = $_POST['MoTa'];

        $AnHien = ($_POST['AnHien'] == "on") ? 0 : 1;
        $NoiBat = ($_POST['NoiBat'] == "on") ? 1 : 0;
        $trangthai = ($_POST['trangthai'] == "on") ? 0 : 1;

        $ngay = strtotime('now');

        if ($thanhcong == false) {
            return $thanhcong;
        } else {
            $sql = "INSERT INTO sanpham
					VALUES(NULL,'$MaSP',$TenSP','$TenSP_KD','$Gia','$MoTa','$AnHien','$NoiBat',
                    '$ngay','$UrlHinh',$idTL,$idLT,$trangthai)";
            mysql_query($sql) or die(mysql_error() . $sql);            
        }
        return $thanhcong;
    }

    function addImages($url, $idSP) {
        $sql = "INSERT INTO hinhsp VALUES(NULL,'$url',$idSP)";
        mysql_query($sql);
    }

    function SanPham_Sua($sp_id, &$loi) {
        settype($sp_id, "int");
        $thanhcong = true;
        $idLT = (int) $_POST['idLT'];
        $idTL = (int) $_POST['idTL'];
        $MaSP = $this->processData($_POST['MaSP']);
        $TenSP = $this->processData($_POST['TenSP']);
        $TenSP_KD = $this->changeTitle($TenSP);
        $Gia = $this->processData($_POST['Gia']);
       
        $UrlHinh = $this->processData($_POST['UrlHinh']);
        
        $MoTa = $_POST['MoTa'];

         $AnHien = ($_POST['AnHien'] == "on") ? 0 : 1;
        $NoiBat = ($_POST['NoiBat'] == "on") ? 1 : 0;
        $trangthai = ($_POST['trangthai'] == "on") ? 0 : 1;

        $ngay = strtotime('now');

        if ($thanhcong == false) {
            return $thanhcong;
        } else {
            $sql = "UPDATE sanpham SET MaSP = '$MaSP',
                                TenSP = '$TenSP',TenSP_KD = '$TenSP_KD',Gia = '$Gia',MoTa ='$MoTa',
                                AnHien = '$AnHien',NoiBat = '$NoiBat',
                                trangthai = '$trangthai',ngay = '$ngay',UrlHinh = '$UrlHinh',idLT = $idLT,idTL = $idTL					
                                WHERE idSP = $sp_id";
            mysql_query($sql) or die(mysql_error() . $sql);           
        }
        return $thanhcong;
    }

    function getDetailSP($sp_id) {
        $sql = "SELECT * 
                FROM sanpham
                WHERE idSP = $sp_id";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }

    function SanPham_List($loai_id = -1, $tukhoa = '', $limit = -1, $offset = -1) {
        $sql = "SELECT * FROM sanpham 
                WHERE (idTL = $loai_id OR $loai_id = -1 )";
        if ($tukhoa != "")
            $sql.=" AND TenSP LIKE '%$tukhoa%' ";
        $sql.="	ORDER BY idSP DESC ";
        if ($limit > 0 && $offset >= 0)
            $sql.= " LIMIT $offset,$limit";
        $rs = mysql_query($sql) or die(mysql_error());
        return $rs;
    }

}

?>